package com.ziyun.pop.modules.userPayment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ziyun.pop.modules.order.entity.UserPaymentEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.pop.modules.userPayment.dao.ErpUserPaymentDao;
import com.ziyun.pop.modules.userPayment.service.UserPaymentService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("userPaymentService")
public class UserPaymentServiceImpl extends ServiceImpl<ErpUserPaymentDao, UserPaymentEntity> implements UserPaymentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserPaymentEntity> page = (Page<UserPaymentEntity>) this.page(
                new Query<UserPaymentEntity>(params).getPage(),
                new QueryWrapper<UserPaymentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserPaymentEntity getPaySuccLastEntityByOrderId(String orderId) {
        return this.baseMapper.getPaySuccLastEntityByOrderId(orderId);
    }

    @Override
    public Integer checkOrderIsPay(String orderId) {
        return this.baseMapper.checkOrderIsPay(orderId);
    }

    @Override
    public Map<String, String> getPaymentById(String orderId, String payType, BigDecimal surplusPayed) {
        Map<String,Object> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("payType", payType);
        params.put("surplusPayed", surplusPayed);
        return this.baseMapper.getPaymentById(params);
    }

    @Override
    public List<UserPaymentEntity> queryOrderPaymentInfo(String orderId) {
        List<UserPaymentEntity> list =  baseMapper.queryOrderPaymentInfo(orderId);
        return list;
    }

    @Override
    public UserPaymentEntity getPaySuccLastEntityByOrderNo(String orderNo) {
        return baseMapper.getPaySuccLastEntityByOrderNo(orderNo);
    }

    /**
     * 扫码支付先写入支付响应码
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,  propagation = Propagation.REQUIRES_NEW)
    public void writePayResponseData(UserPaymentEntity userPaymentEntity){
        this.updateById(userPaymentEntity);
    }

}
